package com.dietdroid;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * Class shows diet data from food items
 * consumed today based on serving size. Progress
 * is determined by the diet profile values. Different
 * dates can be selected and data will be shown
 * from those dates.
 * 
 * @author Mike Lopez
 *
 */
public class HistoryViewActivity extends Activity {
	/*
	 * The current year.
	 */
    private int mYear;
    /*
     * The current month.
     */
    private int mMonth;
    /*
     * The current day.
     */
    private int mDay;
    /*
     * The date listener for date changes
     * in the date change dialog.
     */
    private DatePickerDialog.OnDateSetListener mDateSetListener;
    
    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_view);
        
        //set heading
        final TextView history_date_text =(TextView)findViewById(R.id.history_date_txt);
        history_date_text.setText("Today's Nutrition");
        
        
        //date picker
        Button date_pick = (Button) findViewById(R.id.change_date_but);
        date_pick.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(0);
            }
        });
        
        // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        //populate list
        final ArrayList<NutritionHistoryModel> nutrition_data = DietDataOperations.getDietHistory(mMonth, mDay, mYear, this.getApplicationContext());
        ListView nutrition_fact_list = (ListView) findViewById(R.id.nutrition_list);
        nutrition_fact_list.setAdapter(new NutritionHistoryAdapter(nutrition_data));
        
        //set click listener on nutrition facts
        nutrition_fact_list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            	NutritionHistoryModel model = nutrition_data.get(position);
                Intent myIntent = new Intent(view.getContext(), NutritionFoodHistoryActivity.class);
                
                myIntent.putExtra("YEAR", mYear);
                myIntent.putExtra("MONTH", mMonth);
                myIntent.putExtra("DAY", mDay);
                myIntent.putExtra("NUTRITION_FACT",model.getNutrition_fact());
                
                startActivityForResult(myIntent, 0);
            }
          });

        
         //date change action
         mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    Calendar cal = Calendar.getInstance();
                	
                	if(cal.get(Calendar.YEAR) == year && cal.get(Calendar.MONTH) == monthOfYear
                			&& cal.get(Calendar.DAY_OF_MONTH) == dayOfMonth) {
                		history_date_text.setText("Today's Nutrition");
                	}else {
                		history_date_text.setText("Nutrition for: " + (monthOfYear + 1) + "/" +
                				dayOfMonth + "/" + year);
                	}
            		mYear = year;
            		mMonth = monthOfYear;
            		mDay = dayOfMonth;
                	populateNutritionList(mYear, mMonth, mDay);

                }
            };
            

            //display list of all foods consumed
            Button all_foods = (Button) findViewById(R.id.all_foods_but);
            all_foods.setOnClickListener(new View.OnClickListener() {
            	public void onClick(View view) {
                    Intent myIntent = new Intent(view.getContext(), ConsumedFoodsActivity.class);
                    myIntent.putExtra("YEAR", mYear);
                    myIntent.putExtra("MONTH", mMonth);
                    myIntent.putExtra("DAY", mDay);
                    startActivityForResult(myIntent, 0);
            	}
            });
            
	}
	
	/**
	 * Populate diet data for the given date
	 * 
	 * @param year The year to display data for.
	 * @param month The month to display data for.
	 * @param day The day to display data for.
	 */
	private void populateNutritionList(int year, int month, int day) {
		
        ArrayList<NutritionHistoryModel> nutrition_data = DietDataOperations.getDietHistory(month, day, year, this.getApplicationContext());
        ListView nutrition_fact_list = (ListView) findViewById(R.id.nutrition_list);
        nutrition_fact_list.setAdapter(new NutritionHistoryAdapter(nutrition_data));
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreateDialog(int)
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    case 0:
	        return new DatePickerDialog(this,
	                    mDateSetListener,
	                    mYear, mMonth, mDay);
	    }
	    return null;
	}
	
	/**
	 * 
	 * Class for creating correct nutrition
	 * list display rows.
	 *
	 */
	private class NutritionHistoryAdapter extends ArrayAdapter<NutritionHistoryModel>{
		/*
		 * The data to display in the list.
		 */
		private ArrayList<NutritionHistoryModel> history_list;
		
		/**
		 * NutritionHistoryAdapter constructor.
		 * 
		 * @param history_list The data to display.
		 */
		public NutritionHistoryAdapter(
				ArrayList<NutritionHistoryModel> history_list) {
			super(HistoryViewActivity.this, R.layout.history_nutrition_row, history_list);
			
			this.history_list = history_list;
			
		}
		
		/*
		 * (non-Javadoc)
		 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
		 */
	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	View v = convertView;
	    	NutritionHistoryModel model = history_list.get(position);  
	    	if (v == null) {
	    		LayoutInflater inflater = ((Activity)HistoryViewActivity.this).getLayoutInflater();
	    		v = inflater.inflate(R.layout.history_nutrition_row, parent, false);
		    	TextView bt = (TextView) v.findViewById(R.id.nutrition_amount_text);
		    	bt.setText(model.getTo_display());
		    	
		    	if(model.getCurrent_amount()>model.getMax_amount()) {
		    		bt.setTextColor(Color.RED);
		    		System.out.println(position);
		    	}
		    	
		    	TextView tt = (TextView) v.findViewById(R.id.nutrition_fact_txt);
		    	ProgressBar pb = (ProgressBar) v.findViewById(R.id.nutrition_progress);
		    	tt.setText(model.getNutrition_fact());                            
		    	pb.setMax(model.getMax_amount());
		    	pb.setProgress(model.getCurrent_amount());
	    	}  	
	    	return v;
	    }
	}

}
